package com.example.zhujiemian.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.zhujiemian.Bean.Student;


public class DBHelper extends SQLiteOpenHelper {

    private static final String TAG = "DBHelper";
    private static final String TABLE_NAME_STUDENT = "student";
    private static final String TABLE_NAME_COURSE = "course";
    private static DBHelper mHelper = null;

    Context mcontext;
    public DBHelper(@Nullable Context context) {
        super(context, "ADDinformation", null, 1);
        this.mcontext=context;
    }
    private void initData(SQLiteDatabase db) {
        db.execSQL("insert into " + TABLE_NAME_STUDENT +
                " (student_name , student_code, username, password, class_name) " +
                "values" +
                "('张老三', '202100000001', 'helloworld', '12345678', '计算机1班')");
        db.execSQL("insert into " + TABLE_NAME_STUDENT +
                " (student_name , student_code, username, password, class_name) " +
                "values" +
                "('李老四', '202100000002', 'helloworld', 'abcdefgh', '计算机2班')");

        db.execSQL("insert into " + TABLE_NAME_COURSE +
                " (course_name , course_code, course_teacher, learning_hours) " +
                "values" +
                "('Android', '99996666', '张老师', '80')");
    }
    public void insertStudent(Student student) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("insert into " + TABLE_NAME_STUDENT +
                " (student_name , student_code, username, password, class_name) " +
                "values" +
                "('" + student.getStudent_name() + "','" + student.getStudent_code()
                + "','" + student.getUsername() + "','" + student.getPassword() +
                "','" + student.getClass_name() + "')");
    }
    public int insertStudent2(Student student) {
        //获取可写数据库
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("student_name", student.getStudent_name());
        values.put("student_code", student.getStudent_code());
        values.put("username", student.getUsername());
        values.put("password", student.getPassword());
        values.put("class_name", student.getClass_name());
        long newRowId = db.insert(TABLE_NAME_STUDENT, null, values);
        Log.d(TAG, "insertStudent2 newRowId = " + newRowId);
        return (int) newRowId;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        initTable(db);
        initData(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public static DBHelper getInstance(Context context){
        if(mHelper == null){
            mHelper = new DBHelper(context);
        }
        return mHelper;
    }
    private void initTable(SQLiteDatabase db){
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_STUDENT + " (" +
                "student_id integer primary key autoincrement," +
                "student_name text," +
                "student_code text," +
                "username text," +
                "password text," +
                "class_name text)");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_COURSE + " (" +
                "course_id integer primary key autoincrement," +
                "course_name text," +
                "course_teacher text," +
                "studytime text)");
    }
}
